﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RestaurantBooker.Web.Data;
using System.Data.Linq;
using System.Data.Linq.SqlClient;

namespace RestaurantBooker.Web.Services
{
    /// <summary>
    /// Extenion metods for my service
    /// </summary>
    public partial class RestaurantBookerDomainService
    {
        /// <summary>
        /// Return Orders for period and room
        /// Will be included ...
        /// </summary>
        /// <param name="roomId"></param>
        /// <param name="fromDateTime"></param>
        /// <param name="toDateTime"></param>
        /// <returns></returns>
        public IQueryable<Order> GetOrderForPeriodAndRoom(int roomId, DateTime fromDateTime, DateTime toDateTime)
        {
            //DataLoadOptions options = new DataLoadOptions();
            //options.LoadWith<Order>(o => o.);            
            //this.DataContext.LoadOptions = options;
            
            var tablePlaceQuery = (from rt in this.DataContext.Room_Tables
                                  join tp in this.DataContext.Table_Places on rt.room_table_id equals tp.room_table_id
                                  where rt.room_id == roomId
                                  select tp.table_place_id);

            var orderQuery =
                (from order in this.DataContext.Orders
                join od in this.DataContext.Dish_Orders on order.order_id equals od.order_id
                where
                tablePlaceQuery.Contains(od.table_place_id.Value)
                && (
                (order.order_start_time >= fromDateTime && order.order_start_time <= toDateTime)
                        //|| (order.order_start_time.AddMinutes(order.order_duration) >= fromDateTime && order.order_start_time.AddMinutes(order.order_duration) <= toDateTime)
                  ||  ( SqlMethods.DateDiffMinute(fromDateTime, order.order_start_time) <= 0)                        
                )
                select order).Distinct();
            //var s = orderQuery.Provider.ToString();
            return orderQuery;
        }


        public IQueryable<Room_Table> GetRoomTablesWithPlaces(int roomId)
        {
            //TODO:  Do not forget - Include attribute must exist!!!!
            DataLoadOptions options = new DataLoadOptions();
            options.LoadWith<Room_Table>(rt => rt.Table_Places);            
            this.DataContext.LoadOptions = options;
            
            return null;
        }

        public IQueryable<Room_Table> GetRoom_TablesQueryWithTabelPlases()
        {
            // Define the child entries to get
            DataLoadOptions options = new DataLoadOptions();
            options.LoadWith<Room_Table>(b => b.Table_Places);            
            this.DataContext.LoadOptions = options;


            return  this.DataContext.Room_Tables;
        }
    
        public IQueryable<Booker> GetBookersWithRestaurantAndRooms()
        {
            //TODO: do not forget that "[Include]" attribute must exist!!!!            


            // Define the child entries to get
            DataLoadOptions options = new DataLoadOptions();
            options.LoadWith<Booker>(b => b.Restaurant);
            options.LoadWith<Restaurant>(r => r.Rooms);
            this.DataContext.LoadOptions = options;

            var res = this.DataContext.Bookers;
            return res;
        }
    }
}